package com.fubang.rxjavalearning.showapp;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fubang.rxjavalearning.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

public class ShowAppActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.app_list_rv)
    RecyclerView mAppListRv;
    @BindView(R.id.pull_down_srl)
    SwipeRefreshLayout mPullDownSrl;
    private Unbinder mBind;
    private ArrayList<AppInfo> mAppInfoList;
    private AppListAdapter mAppListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_apps);
        mBind = ButterKnife.bind(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mAppListRv.setLayoutManager(linearLayoutManager);
        mAppInfoList = new ArrayList<>();
        mAppListAdapter = new AppListAdapter(mAppInfoList);
        mAppListRv.setAdapter(mAppListAdapter);

        mPullDownSrl.setOnRefreshListener(this);
        mPullDownSrl.post(new Runnable() {
            @Override
            public void run() {
                mPullDownSrl.setRefreshing(true);
                onRefresh();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBind.unbind();
    }

    @Override
    public void onRefresh() {
        if (mAppInfoList != null) {
            mAppInfoList.clear();
            mAppListAdapter.notifyDataSetChanged();
        }
        loadApp();
    }

    private void loadApp() {
        final PackageManager pm = this.getPackageManager();
        Observable.create(new Observable.OnSubscribe<ApplicationInfo>() {
            @Override
            public void call(Subscriber<? super ApplicationInfo> subscriber) {
                List<ApplicationInfo> installedApplications = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
                for (ApplicationInfo info : installedApplications) {
                    subscriber.onNext(info);
                }
                subscriber.onCompleted();
            }
        }).filter(new Func1<ApplicationInfo, Boolean>() {
            @Override
            public Boolean call(ApplicationInfo applicationInfo) {
                return (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0;
            }
        }).map(new Func1<ApplicationInfo, AppInfo>() {
            @Override
            public AppInfo call(ApplicationInfo applicationInfo) {
                AppInfo appInfo = new AppInfo();
                appInfo.setAppIcon(applicationInfo.loadIcon(pm));
                appInfo.setAppName(applicationInfo.loadLabel(pm).toString());
                return appInfo;
            }
        }).subscribe(new Subscriber<AppInfo>() {
            @Override
            public void onCompleted() {
                mAppListAdapter.notifyDataSetChanged();
                mPullDownSrl.setRefreshing(false);
            }

            @Override
            public void onError(Throwable e) {
                mAppListAdapter.notifyDataSetChanged();
                mPullDownSrl.setRefreshing(false);
            }

            @Override
            public void onNext(AppInfo appInfo) {
                mAppInfoList.add(appInfo);
            }
        });


    }
}
